<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input placeholder="配置项名称" v-model="dataForm.paramName" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-input placeholder="代码" v-model="dataForm.paramNo" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-select placeholder="类型" v-model="dataForm.paramType" clearable>
            <el-option v-for="(item,key) of this.dicts['param_type'].childMap" :key="key" :label="item" :value="key" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList(1)">
            <fa-icon class="fa-search" />查询</el-button>
          <reset-button :dataForm.sync="dataForm" :fixedData="{pageNum: 1, pageSize: 20}" />
          <el-button size="small" plain @click="exportExcelEvent">
            <fa-icon class="fa-download" />导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in BIZ_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editRow(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-dialog title="编辑配置项" :visible.sync="editDialog" width="30%" :close-on-click-modal="false">
      <edit-biz v-if="editDialog" @close="closeEditPop" :listValue="userValueList" @showType="getEditType" :initData="editData" :isTag="isTag" :isUser="isUser" :options="BIZ_FORM" @updataSuccess="uppdataSuccess" @changeSelect="getSelectValue" />
    </el-dialog>
    <el-dialog title="编辑配置项" :visible.sync="editDialog1" width="30%" :close-on-click-modal="false">
      <edit-biz v-if="editDialog1" :initData="editData" :options="BIZ_FORM_1" @updataSuccess="uppdataSuccess"  @close="closeEditPop"/>
    </el-dialog>
    <el-dialog title="编辑成员" :visible.sync="editUser" width="30%" :close-on-click-modal="false">
      <choose-user v-if="editUser" :valueList="userValueList" :initData="editUser" @updataSuccess="getUserData" @close="closeUserChoose" />
    </el-dialog>
    <el-dialog title="编辑标签" :visible.sync="editTag" width="30%" :close-on-click-modal="false">
      <choose-tag v-if="editTag" :valueList="userValueList" :initData="editTag" @updataSuccess="getTagData" @close="closeTagChoose" />
    </el-dialog>
  </div>
</template>

<script>
import { getUserCookie } from '@/utils/auth'
import { mapState } from 'vuex'
import { BIZ_TYPE, BIZ_HEAD_LIST, BIZ_FORM, BIZ_FORM_1 } from './js/contant.js'
import { CONSTANT_OBJ } from '@/utils/constant'
import EditBiz from './components/EditBiz'
import ChooseUser from './components/ChooseUser'
import ChooseTag from './components/ChooseTag'
import { exportExcel } from '@/utils/util'
import api from './api'
const SETTING_TYPE = {
  'examine_needed_apply': 'CASE_AUDIT',
  'examine_needed_loan': 'LOAN_AUDIT',
  'exception_notify_phone': 'ERROR_NOTIFY',
}
export default {
  components: { EditBiz, ChooseUser, ChooseTag },
  data () {
    return {
      BIZ_TYPE,
      BIZ_HEAD_LIST,
      BIZ_FORM,
      BIZ_FORM_1,
      SETTING_TYPE,
      editDialog: false,
      editDialog1: false,
      tableLoading: false,
      isTag: false,
      isUser: false,
      editUser: false,
      editTag: false,
      editData: {},
      dataForm: {
        paramName: '',
        paramNo: '',
        paramType: '',
        pageNum: 0,
        pageSize: 10
      },
      tagForm: {
        // labelName: '',
        // pageNum: 1,
        // pageSize: 10
      },
      dataList: [],
      total: 0,
      userValueList: []
    }
  },
  mounted () {
    this.getList()
    this.init()
  },
  methods: {
    init () {
      this.BIZ_FORM.forEach(ele => {
        if (ele.prop === 'paramType') {
          ele.option = {...this.dicts['param_type'].childMap}
        }
        if (ele.prop === 'paramValue') {
          ele.option = {...this.dicts['director_params'].childMap}
        }
      })
      this.BIZ_FORM_1.forEach(ele => {
        if (ele.prop === 'paramType') {
          ele.option = {...this.dicts['param_type'].childMap}
        }
      })
    },
    // 获取列表数据
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.postQueryBizData(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.dataList.forEach(ele => {
            ele.newParamType = this.dicts['param_type'].childMap[ele.paramType]
            ele.newParamValue = this.dicts['director_params'].childMap[ele.paramValue] ? this.dicts['director_params'].childMap[ele.paramValue] : ele.paramValue
            ele.newOldParamValue = this.dicts['director_params'].childMap[ele.paramOldValue] ? this.dicts['director_params'].childMap[ele.paramOldValue] : ele.paramOldValue
          })
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 编辑
    editRow (row) {
      const { id, paramName, paramNo, remark, paramType, paramValue } = row
      this.editData = { id, paramName, paramNo, remark, paramType, paramValue }
      if (paramNo === 'examine_needed_loan' || paramNo === 'examine_needed_apply' || paramNo === 'exception_notify_phone') {
        this.editDialog = true
      } else {
        this.editDialog1 = true
      }
      console.log(paramValue, '打开编辑')
      if (paramValue === 'SELECT_USER') { // 人员
        this.isTag = false
        this.isUser = true
        this.queryShowList()
      } else if (paramValue === 'SELECT_LABEL') { // 标签
        this.isTag = true
        this.isUser = false
        this.queryShowList()
      } else {
        this.isTag = false
        this.isUser = false
      }
    },
    queryShowList () {
      const { paramValue } = this.editData
      api.getSettingData({settingId: this.editData.id, receiveSett: paramValue}).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          console.log(data)
          if (paramValue === 'SELECT_USER') { // 人员
            data.forEach(ele => {
              ele.userName = ele.listValue
              ele.realName = ele.listValueName
            })
            this.userValueList = [...data]
          } else if (paramValue === 'SELECT_LABEL') { // 标签
            this.getAllTagList(data)
          }
        }
      })
    },
    // 获取全部标签
    getAllTagList (list) {
      // this.tagForm.pageNum = val || this.tagForm.pageNum
      this.tableLoading = true
      api.postQueryLabelData(this.tagForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          let res = []
          let ids = list.map(ele => {
            return ele.listValue
          })
          data.list.forEach(el => {
            if (ids.includes(el.labelId)) {
              res.push({
                ...el,
                settingId: this.editData.id,
                settingType: this.SETTING_TYPE[this.editData.paramNo],
                listValue: el.labelId,
                listValueName: el.labelName
              })
            }
          })
          this.userValueList = [...res]
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 获取全部人员
    // getAllUserList (list) {
    //   this.tableLoading = true
    //   api.queryAllUserList({}).then(res => {
    //     const { responseCode, data } = res
    //     if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
    //       let res = []
    //       let ids = list.map(ele => {
    //         return ele.listValue
    //       })
    //       data.list.forEach(el => {
    //         if (ids.includes(el.userName)) {
    //           res.push({
    //             ...el,
    //             settingId: this.editData.id,
    //             settingType: this.SETTING_TYPE[this.editData.paramNo],
    //             listValue: el.userName
    //           })
    //         }
    //       })
    //       console.log(res, '用户过滤')
    //       this.userValueList = [...res]
    //     }
    //   }).finally(() => {
    //     this.tableLoading = false
    //   })
    // },
    // 编辑返回
    uppdataSuccess (data) {
      console.log(data, '编辑提交')
      let { tenantId } = getUserCookie()
      data.tenantId = tenantId
      api.putBizData(data).then(res => {
        const { responseCode, responseMsg: message } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message,
            type: 'success'
          })
          this.editDialog = false
          this.editDialog1 = false
          this.getList(1)
        }
      })
    },
    getSelectValue (val) {
      console.log(val, '获取选择结果')
      this.userValueList = []
      switch (val) {
        case 'SELECT_LABEL':
          this.isTag = true
          this.isUser = false
          break
        case 'SELECT_USER':
          this.isTag = false
          this.isUser = true
          break
        default:
          this.isTag = false
          this.isUser = false
          break
      }
    },
    getEditType (val) {
      console.log(val, '类别')
      switch (val) {
        case 'user':
          this.editUser = true
          break
        case 'tag':
          this.editTag = true
          break
      }
    },
    closeUserChoose (val) {
      this.editUser = val
    },
    closeTagChoose (val) {
      this.editTag = val
    },
    getUserData (data) {
      let res = data.map(ele => {
        ele.settingId = this.editData.id
        ele.settingType = this.SETTING_TYPE[this.editData.paramNo]
        ele.listValue = ele.userName
        return ele
      })
      console.log(res, '用户结果')
      this.userValueList = [...res]
      this.editUser = false
    },
    getTagData (data) {
      let res = data.map(ele => {
        ele.settingId = this.editData.id
        ele.settingType = this.SETTING_TYPE[this.editData.paramNo]
        ele.listValue = ele.labelId
        return ele
      })
      console.log(res, '标签结果')
      this.userValueList = [...res]
      this.editTag = false
    },
    closeEditPop (val) {
      this.editDialog = false
      this.editDialog1 = false
    },
    // 导出配置项明细this.editDialog = false
    exportExcelEvent () {
      console.log('导出')
      let { tenantId } = getUserCookie()
      // 暂无导出接口
      let parmas = {
        paramNo: '',
        paramName: '',
        paramType: '',
        tenantId
      }
      let path = `/v1/api/backend/business/param/export`
      let name = '业务配置'
      exportExcel(path, parmas, name)
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>
